/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.utils;

import ca.trader.user.User;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Ahsan
 */
public class HttpUtils
{

	 public static void setUserAuthenticated(HttpSession session, User user)
	 {
		  session.setMaxInactiveInterval(HttpConsts.MAX_USER_LOGIN_SESSION_LENGTH);

		  session.setAttribute(HttpConsts.SESSION_ATTRIBUTE_AUTHENTICATED_USER_EMAIL, user.getEmail());
		  session.setAttribute(HttpConsts.SESSION_ATTRIBUTE_AUTHENTICATED_USER_FIRST_NAME, user.getFirstName());
		  session.setAttribute(HttpConsts.SESSION_ATTRIBUTE_AUTHENTICATED_USER_ID, user.getId());
		  session.setAttribute(HttpConsts.SESSION_ATTRIBUTE_USER_AUTHENTICATED, true);
	 }


	 

	 public static String getCookieValue(HttpServletRequest request, String name)
	 {
		  Cookie[] cookies = request.getCookies();
		  if (cookies != null)
		  {
			   for (Cookie cookie : cookies)
			   {
				    if (name.equals(cookie.getName()))
				    {
						return cookie.getValue();
				    }
			   }
		  }
		  return null;
	 }

	 public static void addCookie(HttpServletResponse response, String name, String value, int maxAge)
	 {
		  Cookie cookie = new Cookie(name, value);
		  cookie.setMaxAge(maxAge);
		  response.addCookie(cookie);
	 }

	 public static void removeCookie(HttpServletResponse response, String name)
	 {
		  addCookie(response, name, null, 0);
	 }

	 public static boolean wasUserAuthenticationFailed(HttpSession session)
	 {
		  return session.getAttribute(HttpConsts.SESSION_ATTRIBUTE_USER_AUTHENTICATED) != null && !((Boolean) session.getAttribute(HttpConsts.SESSION_ATTRIBUTE_USER_AUTHENTICATED));
	 }

	 public static boolean wasUserAuthenticationSuccess(HttpSession session)
	 {
		  return session.getAttribute(HttpConsts.SESSION_ATTRIBUTE_USER_AUTHENTICATED) != null && ((Boolean) session.getAttribute(HttpConsts.SESSION_ATTRIBUTE_USER_AUTHENTICATED));
	 }

	 

	

	 public static void sendActivationEmail(HttpServletRequest request, String email, String activationID) throws AddressException, MessagingException
	 {
		  String _methodName = "sendActivationEmail";
		 // logger.logMethodEntry(_methodName);

		  Properties props = new Properties();
		  props.put("mail.transport.protocol", "smtp");
		  Session s = Session.getInstance(props);

		  MimeMessage message = new MimeMessage(s);

		  String emailFrom = "test@trader.com";

		  //logger.logInfo(_methodName, "Sending email from " + emailFrom + ", to " + email);
		  InternetAddress from = new InternetAddress(emailFrom);
		  message.setFrom(from);
		  InternetAddress to = new InternetAddress(email);
		  message.addRecipient(Message.RecipientType.TO, to);

		  message.setSubject("Test from JavaMail.");
		  message.setText("Hello from JavaMail!");
		  Transport.send(message);

		 // logger.logInfo(_methodName, "Sent email");

		//  logger.logMethodExit(_methodName);

	 }

}
